<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2017/3/11
 * Time: 9:36
 */
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\ActiveDataProvider;
use common\models\Goods;
use common\models\Onsales;

class HdgoodsController extends Controller
{
    public function actionIndex()
    {
        if (Yii::$app->user->isGuest)
        {
            $this->redirect(['site/login']);
        }
        $model = new Goods();
        $dataProvider = new ActiveDataProvider([
            'query' => Goods::find()->where(['status' => 1, 'on_sales' => 1]),
        ]);
        return $this->render('index',['model' => $model,  'dataProvider' => $dataProvider]);
    }

    public function actionView()
    {
        $id = Yii::$app->request->get('id');
        if (!preg_match('/^\d+$/', $id))
        {
            $this->redirect(['hdgoods/index']);
        }
        $model = new Goods();
        $arr_goods = $model->getGoods($id);
        return $this->render('view',['arr_goods' => $arr_goods]);
    }

    public function actionOnsales()
    {
        $arr_cb = Yii::$app->request->post('intro_cb', array());
        $arr_tx = Yii::$app->request->post('intro_tx', array());
        $goods_id = Yii::$app->request->post('goods_id', 0);
        list($channel, $goods_id, $id) = explode('/', $goods_id);
        $goods_id = "$channel/$goods_id";
        $total_str = "";
        foreach($arr_cb as $_cbv) {
            $str_pic = base64_decode($arr_tx[$_cbv]);
            preg_match_all('/<img.*src=["|\'](.*)["|\'].*\/>/U', $str_pic, $arr_pic);
            foreach($arr_pic[1] as $_pic) {
                if (empty($_pic)) continue;
                if (@fopen($_pic, 'r')) {
                    $base_name = basename($_pic);
                    if (!file_exists(Yii::getAlias('@frontend')."/web/goods/$goods_id/")) {
                        $dir = @mkdir(Yii::getAlias('@frontend') . "/web/goods/$goods_id/", '666', true);
                    } else {
                        $dir = true;
                    }
                    if ($dir) {
                        if (file_exists(Yii::getAlias('@frontend') . "/web/goods/$goods_id/$base_name"))
                            unlink(Yii::getAlias('@frontend') . "/web/goods/$goods_id/$base_name");
                        @copy($_pic, Yii::getAlias('@frontend') . "/web/goods/$goods_id/$base_name");
                        $str_pic = str_replace($_pic, "/goods/$goods_id/$base_name", $str_pic);
                    }
                }
            }
            $total_str = $total_str . $str_pic;;
        }
        list($channel, $goods_id) = explode('/', $goods_id);
        $model = new Goods();
        $arr_onsalses = $model->findOne(['goods_id' => $goods_id, 'channel' => $channel])->toArray();
        $big_pic = $small_pic = $original_pic = $tiny_pic = $thumbnail_pic = '';
        if ($arr_onsalses) {
            $big_pic = $this->copy_picture($arr_onsalses['big'], $channel, $goods_id);
            $small_pic = $this->copy_picture($arr_onsalses['small'], $channel, $goods_id);
            $original_pic = $this->copy_picture($arr_onsalses['original'], $channel, $goods_id);
            $tiny_pic = $this->copy_picture($arr_onsalses['tiny'], $channel, $goods_id);
            $thumbnail_pic = $this->copy_picture($arr_onsalses['thumbnail'], $channel, $goods_id);
        }
        $onsales = new Onsales();
        $arr_sales = $onsales->findOne(['goods_id' => $id]);
        if ($arr_sales) {
            $onsales->goods_name = $arr_onsalses['goods_name'];
            $onsales->life = $arr_onsalses['life'];
            $onsales->mktprice = $arr_onsalses['mktprice'];
            $onsales->trade_type_name = $arr_onsalses['trade_type_name'];
            $onsales->gid = $id;
            $onsales->goods_id = $goods_id;
            $onsales->picture = json_encode(array('big' => $big_pic, 'small' => $small_pic, 'original' => $original_pic, 'tiny' => $tiny_pic, 'thumbnail' => $thumbnail_pic));
            $onsales->intro = $total_str;
            $onsales->percentage = 1.5;
            $onsales->status = 1;
            $onsales->update();
        } else {
            $onsales->goods_name = $arr_onsalses['goods_name'];
            $onsales->life = $arr_onsalses['life'];
            $onsales->mktprice = $arr_onsalses['mktprice'];
            $onsales->trade_type_name = $arr_onsalses['trade_type_name'];
            $onsales->gid = $id;
            $onsales->goods_id = $goods_id;
            $onsales->picture = json_encode(array('big' => $big_pic, 'small' => $small_pic, 'original' => $original_pic, 'tiny' => $tiny_pic, 'thumbnail' => $thumbnail_pic));
            $onsales->intro = $total_str;
            $onsales->percentage = 1.5;
            $onsales->status = 1;
            $onsales->insert();
        }
        $this->redirect(array('/hdgoods/index'));
    }

    public function copy_picture($url, $channel, $goods_id)
    {
        if (empty($url)) return false;
        if (@fopen($url, 'r')) {
            $path = Yii::getAlias('@frontend') . "/web/goods/$channel/$goods_id";
            if (!file_exists($path)) {
                $dir = @mkdir($path, '666', true);
            } else {
                $dir = true;
            }
            if ($dir) {
                $base_name = basename($url);
                if (file_exists($path."/$base_name")) unlink($path."/$base_name");
                $copy = @copy($url, $path."/$base_name");
                if ($copy) return $base_name;
            }
        }
        return false;
    }
}